package com.nettest.entity.system;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.nettest.entity.BaseAuditable;

/**
 * @author Nick Mao
 * 
 */
@Entity
@Table(name = "t_staff")
public class Staff extends BaseAuditable implements UserDetails {

	private static final long serialVersionUID = -7362637335011670295L;
	@TableGenerator(
			name="idGen", 
			table="ID_GENERATOR", 
			pkColumnName="ID_KEY", 
			valueColumnName="ID_VALUE", 
			pkColumnValue="STAFF_ID", 
			allocationSize=1)
	@Id
	@GeneratedValue(strategy=GenerationType.TABLE, generator="idGen")
	@Column(name="id")
	public Long id;
	@Column(name = "staff_no",length=100,nullable=false)
	private String staffNo;
	@Column(name = "pass_word",length=100,nullable=false)
    private String password;
	@Column(name = "staff_name",length=100,nullable=false)
    private String staffName;
	@Column(name = "dept_id",length=100)
    private String deptId;
    @Column(name = "position",length=100)
    private String position;
    @Column(name = "address",length=400)
    private String address;
    @Column(name = "telephone",length=100)
    private String telephone;
    @Column(name = "mobile",length=100)
    private String mobile;
    @Column(name = "qq",length=100)
    private String qq;
    @Column(name = "email",length=100)
    private String email;
    @Column(name = "sex",length=20)
    private String sex;
    @Column(name = "employ_date")
    @Temporal(value=TemporalType.TIMESTAMP)
    private Date employDate;
    @Column(name = "memo",length=4000)
    private String memo;
    @Column(name = "active")
    @org.hibernate.annotations.Type(type="yes_no")
    private Boolean active;
    @Transient
    private String deptName;
    @Transient
    private String roleName;
    @Transient
    private String roleId;
    @Transient
    private String activeStr;
    @Transient
    private Collection<GrantedAuthority> authorities;// 角色信息
	
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "t_staff_role", joinColumns = @JoinColumn(name = "staff_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	@OrderBy("roleName asc")
	private Set<Role> roles = new HashSet<Role>();
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getStaffNo() {
		return staffNo;
	}

	public void setStaffNo(String staffNo) {
		this.staffNo = staffNo;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getEmployDate() {
		return employDate;
	}

	public void setEmployDate(Date employDate) {
		this.employDate = employDate;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public Boolean getActive() {
		active = "Y".equals(activeStr) ? true : false;
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	@Override
	public String getUsername() {
		return staffName;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return active == null ? false : active;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getActiveStr() {
		return activeStr;
	}

	public void setActiveStr(String activeStr) {
		this.activeStr = activeStr;
	}
}
